package com.crazyang.common.interceptor;

import com.crazyang.entity.User;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * @ClassName MyInterceptor
 * @Description: MyInterceptor
 * @Author zhouyang
 * @Date 2019/2/28 上午10:56.
 */
public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        String ip = request.getRemoteAddr();
//        long startTime = System.currentTimeMillis();
//        request.setAttribute("requestStartTime", startTime);
////        HandlerMethod handlerMethod = (HandlerMethod) handler;
////        Method method = handlerMethod.getMethod();
////        System.out.println("用户:"+ip+",访问目标:"+method.getDeclaringClass().getName() + "." + method.getName());
//
//        User user=(User)request.getSession().getAttribute("user");
//        if(null==user){
//            response.sendRedirect("login");
//           return false;
//        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        HandlerMethod handlerMethod = (HandlerMethod) handler;
//        Method method = handlerMethod.getMethod();
//        long startTime = (Long) request.getAttribute("requestStartTime");
//        long endTime = System.currentTimeMillis();
//        long executeTime = endTime - startTime;
//        // 打印方法执行时间
//        if (executeTime > 1000) {
//            System.out.println("[" + method.getDeclaringClass().getName() + "." + method.getName() + "] 执行耗时 : "
//                    + executeTime + "ms");
//        } else {
//            System.out.println("[" + method.getDeclaringClass().getSimpleName() + "." + method.getName() + "] 执行耗时 : "
//                    + executeTime + "ms");
//        }
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
